/**
 * Copyright 2009 - Morten Udnæs
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.javabatch.petstore;

import fitnesse.ComponentFactory;
import fitnesse.FitNesse;
import fitnesse.FitNesseContext;
import fitnesse.authentication.PromiscuousAuthenticator;
import fitnesse.html.HtmlPageFactory;
import fitnesse.responders.ResponderFactory;
import fitnesse.wiki.FileSystemPage;

/**
 * Class to start fitnesse from inside eclipse. Sets needed classpath variable.
 * @author Morten Udnæs
 *
 */
public class FitnesseStarter {

    /** Fitnesseport. */
    private static final int FITNESSE_PORT = 8082;

    /**
     * Main method.
     * @param args paramters
     * @throws Exception anything
     */
    public static final void main(final String[] args) throws Exception {
        FitnesseStarter runFitnesse = new FitnesseStarter();
        runFitnesse.start();
    }

    /**
     * Starting fitnesse.
     * @throws Exception oops
     */
    public final void start() throws Exception {
        FitNesseContext context = loadContext();
        FitNesse fitnesse = new FitNesse(context);
        fitnesse.applyUpdates();

        boolean started = fitnesse.start();
        if (started) {
           System.out.println(context);
        }
    }

    /**
     * Setting up fitnesse.
     *
     * @return Fitnesse context
     * @throws Exception Anything that goes wrong
     */
    protected final FitNesseContext loadContext() throws Exception {
        FitNesseContext context = new FitNesseContext();
        ComponentFactory componentFactory = new ComponentFactory(
                context.rootPath);
        context.port = FITNESSE_PORT;
        context.rootPath = "./src/fitnesse";
        context.rootPageName = "FitNesseRoot";

        context.rootPagePath = context.rootPath + "/" + context.rootPageName;
        context.root = componentFactory.getRootPage(FileSystemPage.makeRoot(
                context.rootPath, context.rootPageName));
        context.responderFactory = new ResponderFactory(context.rootPagePath);
        context.logger = null;
        context.authenticator = componentFactory
                .getAuthenticator(new PromiscuousAuthenticator());
        context.htmlPageFactory = componentFactory
                .getHtmlPageFactory(new HtmlPageFactory());

        System
                .setProperty("eclipsepath", System
                        .getProperty("java.class.path"));

        String extraOutput = componentFactory
                .loadResponderPlugins(context.responderFactory);
        extraOutput += componentFactory.loadWikiWidgetPlugins();
        extraOutput += componentFactory.loadContentFilter();
        return context;
    }

}
